package org.zeqp.chatAI.aiterface.chat;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;
import org.zeqp.chatAI.aiterface.userchat.ChatData;

import java.util.*;

@Component
public class HttpChatUtils {

    private static final String ACCESS_KEY="oEDtlfry7cVWUrfCGz1Ofc3o";
    private static final String PRIVATE_KEY="UvKnsTGqBCE0MCZFQapElYcHPx6QM88z";

    public static final String ACCESSTOKENURL="https://aip.baidubce.com/oauth/2.0/token";

    public static final String STARTCHATURL="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro";

    public static final String SYSTEM_ROBOT_DERACATION="大家好，我是工业小智，一个由中南智能工业智能体创新研究院训练的大型语言模型。我被设计用于在工业层面上与用户进行对话，并提供各种信息和帮助。我可以回答各种问题，提供建议，进行讨论，甚至进行创造性的写作。我可以理解工业上的多种场景，并且可以适应不同的主题和领域。我很高兴能够与大家交流，希望能够为您提供满意的答案和支持！请回复时遵循以上设定，一定不能脱离你的人设";

    public static ChatData sendChatData(String access_token, String userId, String content, List<SendData> history){
        Map<String,Object> data = new HashMap<>();
        SendData curMessage = new SendData();
        curMessage.setContent(content);
        history.add(curMessage);
        data.put("messages",history);
        data.put("user_id",userId);
        data.put("system",SYSTEM_ROBOT_DERACATION);
        System.out.println(JSONUtil.toJsonStr(data));
        HttpResponse execute = HttpRequest.post(HttpChatUtils.STARTCHATURL+"?access_token="+access_token).
                header(Header.CONTENT_TYPE, "application/json").
                body(JSONUtil.toJsonStr(data)).
                timeout(60000).
                execute();
        //as-r7hqrudx3n
        String body = execute.body();
        ChatResponse chatResponse = JSONUtil.toBean(body, ChatResponse.class);
        SendData curAssistant = new SendData();
        curAssistant.setContent(chatResponse.getResult());
        curAssistant.setRole("assistant");
        history.add(curAssistant);
        ChatData chatData = new ChatData();
        chatData.setUserid(userId);
        chatData.setSessionId(chatResponse.getId());
        chatData.setUserHistoryData(history);
        return chatData;
    }




    public static String getAccessToken(){
        Map<String,Object> data = new HashMap<>();
        data.put("grant_type","client_credentials");
        data.put("client_id",ACCESS_KEY);
        data.put("client_secret",PRIVATE_KEY);
        HttpResponse execute = HttpRequest.post(HttpChatUtils.ACCESSTOKENURL).header(Header.CONTENT_TYPE, "application/json").form(data).timeout(20000).execute();
        String body = execute.body();
        JSONObject entries = JSONUtil.parseObj(body);
        System.out.println(entries.toString());
        if(entries.get("access_token")!=null){
            String access_token = entries.get("access_token").toString();
            return access_token;
        }
        return null;
    }

    public static void main(String[] args) {

        String accessToken = HttpChatUtils.getAccessToken();
       // String accessToken = "24.2727d90879eaf9e135af4723fc25329f.2592000.1705042458.282335-44881088";
        List<SendData> historyData = new ArrayList<>();

        //HttpChatUtils.sendChatData(accessToken, "wyx", "msg", historyData);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要询问的内容:\n");
        int i = 0;
        String msg = "";
        while (!(msg=scanner.nextLine()).equals("exit")){
            ChatData chatData = HttpChatUtils.sendChatData(accessToken, "wyx", msg, historyData);
            SendData sendData = chatData.getUserHistoryData().get(chatData.getUserHistoryData().size() - 1);
            System.out.println("gpt result data:"+sendData.getContent());
        }
    }
}
